<?php
namespace app\admin\controller;
use think\Controller;
use think\Exception;
use think\Log;
use think\Db;
use think\Model;

class AuthRule extends Base
{
    /**
     * 菜单管理列表
     * @author wcy
     * @version 2017年4月18日09:47:26
     */
    public function menuList(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $data = $this->deDataTablesJson();
                $draw=$data['draw'];
                $start=$data["start"];
                $length=$data["length"];
                $search="";
                if(!empty($_POST["search"])){
                    $search=trim($_POST["search"]);
                }
                $data=model("AuthRule")->menuList($start,$length,$search);
                if(!$data){
                    return json(["draw"=>$draw,"recordsTotal"=>0,"recordsFiltered"=>0,"data"=>[]]);
                }else{
                    return json(["draw"=>$draw,"recordsTotal"=>$data["count"],"recordsFiltered"=>$data["count"],"data"=>$data["data"]]);
                }
            }else{
                return view("menulist");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("menulist");
        }
    }
    /**
     * 添加顶级菜单
     * @author wcy
     * @version 2017年4月18日10:11:56
     */
    public function addPmenu(){
      try{
          if($this->request->isAjax()||$this->request->isPost()){
              $data["name"]=$_POST["name"];
              $id=null;
              $count=model("AuthRule")->getMenuName($id,$data["name"]);
              if($count>0){
                  return json(["code"=>-1,"msg"=>"菜单名称重复,请重新输入!"]);
              }
              $res=model("AuthRule")->addMenu($data);
              if($res!==false){
                  return json(["code"=>1,"msg"=>"添加成功"]);
              }else{
                  return json(["code"=>-1,"msg"=>"添加失败"]);
              }
          }else{
              return view("addpmenu");
          }
      }catch (Exception $e){
          Log::record($e->getMessage());
          return view("addpmenu");
      }
    }
    /**
     * 编辑顶级菜单
     * @author wcy
     * @version 2017年4月18日10:11:56
     */
    public function editPmenu(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $id=$_POST["id"] ;
                $data["name"]=$_POST["name"];
                $count=model("AuthRule")->getMenuName($id,$data["name"]);
                if($count>0){
                    return json(["code"=>-1,"msg"=>"菜单名称重复,请重新输入!"]);
                }
                $res=model("AuthRule")->editMenu($id,$data);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"修改成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"修改失败"]);
                }
            }else{
                $id=$_GET["id"];
                $data=model("AuthRule")->getMenuById($id);
                if(count($data)>0){
                    $this->assign("data",$data);
                }
                return view("editpmenu");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("editpmenu");
        }
    }
    /**
     * 添加菜单
     * @author wcy
     * @version 2017年4月18日15:50:40
     */
    public function addMenu(){
         try{
             if($this->request->isPost()||$this->request->isAjax()){
                 $data["pid"]=$_POST["pid"];
                 $data["name"]=$_POST["name"];
                 $data["href"]=$_POST["href"];
                 $id=null;
                 $count=model("AuthRule")->getMenuName($id,$data["name"]);
                 if($count>0){
                     return json(["code"=>-1,"msg"=>"菜单名称重复,请重新输入!"]);
                 }
                 $res=model("AuthRule")->addMenu($data);
                 if($res!==false){
                     return json(["code"=>1,"msg"=>"添加成功"]);
                 }else{
                     return json(["code"=>-1,"msg"=>"添加失败"]);
                 }
             }else{
                 $data=model("AuthRule")->getPmenu();
                 $this->assign("menu_data",$data);
                 return view("addmenu");
             }
         }catch (Exception $e){
             Log::record($e->getMessage());
             return view("addmenu");
         }
    }
    /**
     * 编辑菜单
     * @author wcy
     * @version 2017年4月18日15:50:40
     */
    public function editMenu(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $id=$_POST["id"];
                $data["pid"]=$_POST["pid"];
                $data["name"]=$_POST["name"];
                $data["href"]=$_POST["href"];
                $count=model("AuthRule")->getMenuName($id,$data["name"]);
                if($count>0){
                    return json(["code"=>-1,"msg"=>"菜单名称重复,请重新输入!"]);
                }
                $res=model("AuthRule")->editMenu($id,$data);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"修改成功"]);
                }
                else{
                    return json(["code"=>-1,"msg"=>"修改失败"]);
                }
            }else{
                $id=$_GET["id"];
                $data=model("AuthRule")->getMenuById($id);
                $menu_data=model("AuthRule")->getPmenu();
                $this->assign("menu_data",$menu_data);
                $this->assign("data",$data);
                return view("editmenu");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("editmenu");
        }
    }
    /**
     * 删除菜单
     * @author wcy
     * @version 2017年4月18日11:22:49
     */
    public function delMenu(){
        try{
            $id=$_POST["id"];
            $res=model("AuthRule")->delMenu($id);
            if($res!==false){
                return json(["code"=>1,"msg"=>"删除成功"]);
            }else{
                return json(["code"=>-1,"msg"=>"删除失败"]);
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return json(["code"=>-1,"msg"=>"系统出错,请刷新页面!"]);
        }
    }


    /**
     * 权限列表
     * @author wcy
     * @version 2017年4月19日14:20:54
     */
    public function permissionList(){
        try{
            if($this->request->isPost()||$this->request->isAjax()){
                $data = $this->deDataTablesJson();
                $draw=$data['draw'];
                $start=$data["start"];
                $length=$data["length"];
                $search="";
                if(!empty($_POST["search"])){
                    $search=trim($_POST["search"]);
                }
                $data=model("AuthRule")->permissionList($search,$start,$length);
                if(!$data){
                    return json(["draw"=>$draw,"recordsTotal"=>0,"recordsFiltered"=>0,"data"=>[]]);
                }else{
                    return json(["draw"=>$draw,"recordsTotal"=>$data["count"],"recordsFiltered"=>$data["count"],"data"=>$data["data"]]);
                }
            }else{
                return view("permissionlist");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("permissionList");
        }
    }
    /**
     * 添加权限
     * @author wcy
     * @version 2017年4月19日14:35:49
     */
    public function addPermission(){
        try{
            if($this->request->isAjax()||$this->request->isPost()){
                $data["pid"]=$_POST["pid"];
                $data["ppid"]=$_POST["ppid"];
                $data["name"]=$_POST["name"];
                $data["href"]=$_POST["href"];
                $id=null;
                $count=model("AuthRule")->getMenuName($id,$data["name"]);
                if($count>0){
                    return json(["code"=>-1,"msg"=>"权限名称重复,请重新输入!"]);
                }
                $res=model("AuthRule")->addMenu($data);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"添加成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"添加失败"]);
                }
            }else{
                $menu_data=model("AuthRule")->getPmenu();
                $z_data=model("AuthRule")->getZMenuById(1);
                $this->assign("menu_data",$menu_data);
                $this->assign("z_menu_data",$z_data);
                return view("addpermission");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("addpermission");
        }
    }
    /**
     * 编辑权限
     * @author wcy
     * @version 2017年4月21日14:56:04
     */
    public function editPermission(){
        try{
            if($this->request->isPost()||$this->request->isAjax()){
                $id=$_POST["id"];
                $data["pid"]=$_POST["pid"];
                $data["ppid"]=$_POST["ppid"];
                $data["name"]=$_POST["name"];
                $data["href"]=$_POST["href"];
                $res=model("AuthRule")->editMenu($id,$data);
                if($res!==false){
                    return json(["code"=>1,"msg"=>"修改成功"]);
                }else{
                    return json(["code"=>-1,"msg"=>"修改失败"]);
                }
            }else{
                $id=$_GET["id"];
                $data=model("AuthRule")->getMenuById($id);
                $menu_data=model("AuthRule")->getPmenu();
                $z_data=model("AuthRule")->getZMenuById($data["pid"]);
                $this->assign("data",$data);
                $this->assign("menu_data",$menu_data);
                $this->assign("z_menu_data",$z_data);
                return view("editpermission");
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return view("editpermission");
        }
    }
    /**
     * 删除权限
     * @author wcy
     * @version 2017年4月19日17:17:12
     */
    public function delPermission(){
        try{
            $id=$_POST["id"];
            $res=model("AuthRule")->delMenu($id);
            if($res!==false){
                return json(["code"=>1,"msg"=>"删除成功"]);
            }else{
                return json(["code"=>-1,"msg"=>"删除失败"]);
            }
        }catch (Exception $e){
            Log::record($e->getMessage());
            return json(["code"=>-1,"msg"=>"系统错误,请刷新页面"]);
        }
    }
    /**
     * 获取子菜单
     * @author wcy
     * @version 2017年4月19日14:57:58
     */
    public function onChangeMenu(){
        try{
            $id=$_POST["id"];
            $data=model("AuthRule")->getZMenuById($id);
            if(count($data)>0){
                return json(["code"=>1,"msg"=>"获取成功!","data"=>$data]);
            }else{
                return json(["code"=>-1,"msg"=>"系统错误,请刷新页面!"]);
            }
        }catch (Exception $e){
             return json(["code"=>-1,"msg"=>"系统错误,请刷新页面!"]);
        }
    }

}